package logic

import (
	"context"
	"userrpc/internal/util"

	"userrpc/internal/svc"
	"userrpc/pb/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthLogic) Auth(in *userrpc.AuthRequest) (*userrpc.AuthResponse, error) {
	// todo: add your logic here and delete this line
	err := util.SelAuth(in.Username)
	if err != nil {
		return &userrpc.AuthResponse{
			Code: 400,
			Msg:  err.Error(),
		}, nil
	}
	res, err := util.MonthAuths(in.Name, in.IdCard)
	if err != nil {
		if res != "一致" {
			return &userrpc.AuthResponse{
				Code: 400,
				Msg:  err.Error(),
			}, nil
		}
	}
	//添加数据库
	err = util.SetAuth(in.Username, in.Name, in.IdCard)
	if err != nil {
		return &userrpc.AuthResponse{
			Code: 400,
			Msg:  err.Error(),
		}, nil
	}
	return &userrpc.AuthResponse{
		Code: 200,
		Msg:  "实名成功",
	}, nil
}
